package com.mark.util;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

public class JaxrsUtil {

    public static class JaxrsRuntimeException extends RuntimeException {
        private int statusCode;

        public JaxrsRuntimeException(int statusCode) {
            this.statusCode = statusCode;
        }

        public int getStatusCode() {
            return this.statusCode;
        }
    }

    public static void setHttpServletResponseInternalServerError(HttpServletResponse httpServletResponse) {
        setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }

    public static void setHttpServletResponseStatusCode(HttpServletResponse httpServletResponse, int statusCode) {
        //如果response不为空，表示网络调用，需要通过返回码 通知结果
        //如果response为空，表示本地调用，需要通过 异常 通知结果
        if (httpServletResponse != null) {
            httpServletResponse.setStatus(statusCode);
        } else {
            if (statusCode != Response.Status.OK.getStatusCode() &&
                    statusCode != Response.Status.CREATED.getStatusCode() &&
                    statusCode != Response.Status.NO_CONTENT.getStatusCode())
                throw new JaxrsUtil.JaxrsRuntimeException(statusCode);
        }
    }
}
